/**
 * 
 */
package com.alstudio.ui.module.user;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.alstudio.app.ALLocalEnv;
import com.alstudio.pojo.module.user.User;
import com.alstudio.ui.base.TitleBarActivity;
import com.alstudio.ui.module.setting.SelectRegionActivity;
import com.alstudio.utils.android.color.ALColorUtils;
import com.alstudio.utils.formater.age.ALAgeFormatter;
import com.alstudio.utils.formater.sex.ALSexFormatter;
import com.alstudio.utils.log.ALLog;
import com.alstudio.view.tableview.ALTableView;
import com.alstudio.view.tableview.ALTableView.ALTableViewClickListener;
import com.alstudio.view.tableview.BasicItem;
import com.loovee.imaohu.R;

/**
 * 用户资料详情
 * 
 * @author alonso lee
 * 
 */
public class UserInfoDetailAcitivity extends TitleBarActivity implements
		ALTableViewClickListener {

	private User user;
	private boolean isMyHomePage = false;
	private ALTableView tableView;

	private ArrayList<Intent> intents = new ArrayList<Intent>();

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.TitleBarActivity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		initData();
		setupView(this, getClass().getSimpleName());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.BaseActivity#setupView(android.content.Context,
	 * java.lang.String)
	 */
	@Override
	protected void setupView(Context context, String name) {
		// TODO Auto-generated method stub
		super.setupView(context, name);
		setTitleBarContentView(R.layout.user_info_detail_layout);
		setCenterTextViewMessage(R.string.TxtMyProfileDetail);
		tableView = (ALTableView) findViewById(R.id.aLTableView1);
		// initTableView(tableView);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.BaseActivity#initData()
	 */
	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		user = (User) getIntent().getSerializableExtra("user");
		if (user == null) {
			user = ALLocalEnv.getLocalEnv().getMyProfile();
		}
		if (ALLocalEnv.getBareJid(user.getJid()).equals(
				ALLocalEnv.getLocalEnv().getMyAccount().getUserName())) {
			ALLog.d("查看自己的主頁");
			isMyHomePage = true;
			user = ALLocalEnv.getLocalEnv().getMyProfile();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.BaseActivity#finish()
	 */
	@Override
	public void finish() {
		// TODO Auto-generated method stub
		super.finish();
		intents.clear();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.BaseActivity#onStart()
	 */
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		ALLog.d("用户名" + user.getNick());
		initTableView(tableView);
	}

	/**
	 * 创建tableView
	 * 
	 * @param tableView
	 */
	private void initTableView(ALTableView tableView) {
		tableView.clear();
		intents.clear();
		if (isMyHomePage) {
			// 我的详情界面，需要有v认证
			BasicItem vAuthItem = new BasicItem(getString(R.string.TxtVauth));
			vAuthItem.setDrawable(R.drawable.find_user_icon_vip_big);
			vAuthItem.setItemCountColor(ALColorUtils.getColor(this,
					R.color.base_app_color));

			String vauthState = getString(R.string.TxtTovauth);
			switch (user.getVauthed()) {
			case User.ALREADY_AUTHETICATION:
				// 已认证
				vauthState = getString(R.string.TxtVauthed);
				break;
			case User.AUTHETICATIONING:
				// 认证中
				vauthState = getString(R.string.TxtVauthing);
				break;
			}
			vAuthItem.setCountNum(vauthState);
			tableView.addBasicItem(vAuthItem);
			tableView
					.addSegmentations(R.layout.default_blank_list_segmentation);
			Intent i = new Intent(this, NewVauthActivity.class);
			intents.add(i);
		}

		BasicItem nickItem = new BasicItem(user.getNick());
		nickItem.setMiniTitle(getString(R.string.TxtNick));
		tableView.addBasicItem(nickItem);
		if (isMyHomePage) {
			Intent i = new Intent(this, InputActivity.class);
			intents.add(i);
		} else {
			nickItem.setClickable(false);
		}

		BasicItem sexItem = new BasicItem(
				getString(ALSexFormatter.getSexStringRes(user.getSex())));
		sexItem.setMiniTitle(getString(R.string.TxtSex));
		sexItem.setClickable(false);
		tableView.addBasicItem(sexItem);

		BasicItem ageItem = new BasicItem(user.getAge());
		ageItem.setMiniTitle(getString(R.string.TxtAgeTitle));
		tableView.addBasicItem(ageItem);
		if (isMyHomePage) {
			// 跳转到年龄设置
			Intent i = new Intent(this, EditBirthDayActivity.class);
			intents.add(i);
		} else {
			ageItem.setClickable(false);
		}

		user.setConstellations(ALAgeFormatter.getConstellations(user
				.getBirtyday()));
		BasicItem zaroidItem = new BasicItem(user.getConstellations());
		zaroidItem.setMiniTitle(getString(R.string.TxtZaroidTitle));
		tableView.addBasicItem(zaroidItem);
		if (isMyHomePage) {
			// 跳转到星座设置
			Intent i = new Intent(this, EditBirthDayActivity.class);
			intents.add(i);
		} else {
			zaroidItem.setClickable(false);
		}

		String location = getString(R.string.TxtNoContent);
		if (!TextUtils.isEmpty(user.getProvince())) {
			location = user.getProvince() + " " + user.getCity();
		}
		BasicItem locationItem = new BasicItem(location);
		locationItem.setMiniTitle(getString(R.string.TxtLocationTitle));
		tableView.addBasicItem(locationItem);
		if (isMyHomePage) {
			// 跳转到區域设置
			Intent i = new Intent(this, SelectRegionActivity.class);
			intents.add(i);
		} else {
			locationItem.setClickable(false);
		}

		// 加入分段
		tableView.addSegmentations(R.layout.default_blank_list_segmentation);

		// id
		BasicItem idItem = new BasicItem(ALLocalEnv.getBareJid(user.getJid()));
		idItem.setMiniTitle(getString(R.string.TxtIdTitle));
		idItem.setClickable(false);
		tableView.addBasicItem(idItem);

		// email
		BasicItem emailItem = new BasicItem(user.getEmail());
		emailItem.setMiniTitle(getString(R.string.TxtEmail));
		emailItem.setClickable(isMyHomePage);

		if (isMyHomePage) {

			// 跳转到綁定郵箱
			Intent i = new Intent();
			String emailState = getString(R.string.TxtTovauth);
			switch (user.getEmailIsBind()) {
			case User.EMAIL_UNBIND:
				// 未绑定
				i = new Intent(this, EmailBind.class);
				emailState = getString(R.string.TxtEmailUnBind);
				break;
			case User.EMAIL_UNVERIFY:
				// 未验证
				emailState = getString(R.string.TxtEmailUnVerify);
				i = new Intent(this, EmailSendDetail.class);
				break;
			case User.EMAIL_BINDED:
				// 已绑定
				i = new Intent(this, EmailBind.class);
				i.putExtra("emailbind", 1);
				emailState = getString(R.string.TxtEmailBinded);
				break;

			}
			emailItem.setCountNum(emailState);
			emailItem.setItemCountColor(ALColorUtils.getColor(this,
					R.color.base_app_color));
			intents.add(i);
		} else {
			emailItem.setClickable(false);
		}

		tableView.addBasicItem(emailItem);

		if (isMyHomePage) {
			// 密码
			BasicItem pwdItem = new BasicItem(
					getString(R.string.TxtChangePwdHint));
			pwdItem.setMiniTitle(getString(R.string.TxtLoginAccountPwd));
			pwdItem.setColor(ALColorUtils.getColor(this,
					R.color.edit_text_hint_color));
			tableView.addBasicItem(pwdItem);
			// 跳转到更改密码
			Intent i = new Intent(this, ChangePasswordActivity.class);
			intents.add(i);

		}

		tableView.setClickListener(this);
		tableView.commit();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.view.tableview.ALTableView.ALTableViewClickListener#onClick
	 * (int)
	 */
	@Override
	public void onClick(int index) {
		// TODO Auto-generated method stub
		Intent i = intents.get(index);
		// Intent i = new Intent(this, InputActivity.class);
		startActivity(i);
	}

}
